package cfca.paperless.client.servlet.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import cfca.com.itextpdf.text.BadElementException;
import cfca.com.itextpdf.text.Image;
import cfca.com.itextpdf.text.Rectangle;
import cfca.com.itextpdf.text.pdf.PdfReader;

/**
 * 签章坐标的转换处理。
 * 转换前：左上角为原点，坐标为图片中心点；
 * 转换后：左下角为原点，坐标为图片左下角；
 * 
 * @Description TODO
 * @Author dell
 * @Date 2017-12-28
 * @CodeReviewer TODO
 */
public class CoordinateConvert {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("begin pdf seal by coordinate");

        long start = System.currentTimeMillis();

        String pdfFilePath = "./TestData/tmp.pdf";
        String imageFilePath = "./TestData/zhangfei.png";

        byte[] pdfData = null;// 待签章的PDF文件数据
        byte[] imageData = null;// 待合成的印章图片文件数据

        try {
            pdfData = FileUtils.readFileToByteArray(new File(pdfFilePath));
            imageData = FileUtils.readFileToByteArray(new File(imageFilePath));

        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        float[] deviceXY = new float[2];
        deviceXY[0] = 300.5F;// trustSign插件所获取到的X坐标值
        deviceXY[1] = 200.5F;// trustSign插件所获取到的Y坐标值

        System.out.println("x1=" + deviceXY[0] + "," + "y1=" + deviceXY[1]);

        float[] logicalXY = { 0f, 0f };
        try {
            logicalXY = coordinateFromFrontToBack(pdfData, imageData, deviceXY);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        }

        System.out.println("x2=" + logicalXY[0] + "," + "y2=" + logicalXY[1]);

        long end = System.currentTimeMillis();
        long timeconsume = end - start;
        System.out.println("time consume:" + (timeconsume / 1000.0));

        System.out.println("end pdf seal by coordinate");
    }

    public static float[] coordinateFromFrontToBack(byte[] pdfData, byte[] imageData, float[] deviceXY) throws IOException, BadElementException {

        PdfReader reader = new PdfReader(pdfData);

        Image image = Image.getInstance(imageData);

        float deviceX = deviceXY[0], deviceY = deviceXY[1];

        Rectangle rect = reader.getPageSize(1);
        //float pdfPageWidth = rect.getWidth();
        float pdfPageHeight = rect.getHeight();

        //System.out.println("pdfPageWidth=" + pdfPageWidth + "," + "pdfPageHeight=" + pdfPageHeight);
        
        float x1 = deviceX;
        float y1 = pdfPageHeight - deviceY;

        //System.out.println("x1=" + x1 + "," + "y1=" + y1);

        float imageWidth = image.getWidth();
        float imageHeight = image.getHeight();

        //System.out.println("imageWidth=" + imageWidth + "," + "imageHeight=" + imageHeight);

        float x2 = (float) (x1 - imageWidth * 0.5);
        float y2 = (float) (y1 - imageHeight * 0.5);

        float[] logicalXY = new float[2];
        logicalXY[0] = x2;
        logicalXY[1] = y2;

        return logicalXY;
    }

}
